package com.monitor.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.JsonData;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import com.monitor.model.ElasticSearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@Slf4j
public class ElasticSearchClientTestController {
    @Autowired
    private ElasticsearchClient elasticsearchClient;

    @PostMapping("/range")
    public ResponseEntity searchInRangeOfRequestTime(ElasticSearchParams params) {
        log.info("searchInRangeOfRequestTime params:{}", JSON.toJSONString(params));
        List<Object> httpLogs = null;
        try {
            SearchResponse<Object> httpLogSearchResponse = elasticsearchClient.search(
                    SearchRequest.of(
                            s -> s.index(params.getIndex())
//                                    .sort(sort -> sort.field(field -> field.field("resp_time").order(SortOrder.Desc)))
                                    .query(q -> q.bool(
                                            builder -> builder.filter(
                                                    m -> m.range(
                                                            r -> r.field("req_time.keyword").gte(JsonData.of(params.getStartTime())).lte(JsonData.of(params.getEndTime())))))))
                    , Object.class);
            httpLogs = httpLogSearchResponse.hits().hits().stream().map(Hit::source).collect(Collectors.toList());
            log.info("searchInRangeOfRequestTime results.size:{}", httpLogs.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity(httpLogs, HttpStatus.OK);
    }

    @PostMapping("/index")
    public ResponseEntity index(ElasticSearchParams params) {
        log.info("index params:{}", JSON.toJSONString(params));
        List<Object> httpLogs = null;
        try {
            SearchResponse<Object> httpLogSearchResponse = elasticsearchClient.search(
                    SearchRequest.of(
                            s -> s.index(params.getIndex())
                    )
                    , Object.class);
            httpLogs = httpLogSearchResponse.hits().hits().stream().map(Hit::source).collect(Collectors.toList());
            log.info("searchInRangeOfRequestTime results.size:{}", httpLogs.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity(httpLogs, HttpStatus.OK);
    }

}
